package com.caowang.mhtlj.service;

import com.caowang.mhtlj.controller.Result;
import com.caowang.mhtlj.model.Home;
import com.caowang.mhtlj.model.PrivateCard;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class MhtljService {

    /**
     *  获取牌
     * @param home
     * @return
     */
    public String getCard(Home home) {
        ConcurrentHashMap<Integer,String>  pool =  home.getPool();
        Random random = new Random();
        Integer card = random.nextInt(55) + 1;
        //  池子中存在时发牌
        Boolean b = false;
        try {
            home.getLock().lock();
            b = pool.get(card) != null;
        } finally {
            home.getLock().unlock();
        }
        if (b) {
            String  cardString =pool.get(card);
            pool.remove(card);
            return  cardString;
        } else {
            // 重新获取随机数，发牌
            return getCard(home);
        }
    }


    public void flash(Home home) {
        ConcurrentHashMap<Integer, String> pool = home.getPool();
        pool.put(1, "img/htA.png");
        pool.put(2, "img/ht2.png");
        pool.put(3, "img/ht3.png");
        pool.put(4, "img/ht4.png");
        pool.put(5, "img/ht5.png");
        pool.put(6, "img/ht6.png");
        pool.put(7, "img/ht7.png");
        pool.put(8, "img/ht8.png");
        pool.put(9, "img/ht9.png");
        pool.put(10, "img/ht10.png");
        pool.put(11, "img/htJ.png");
        pool.put(12, "img/htQ.png");
        pool.put(13, "img/htK.png");
        pool.put(14, "img/hongtaoA.png");
        pool.put(15, "img/hongtao2.png");
        pool.put(16, "img/hongtao3.png");
        pool.put(17, "img/hongtao4.png");
        pool.put(18, "img/hongtao5.png");
        pool.put(19, "img/hongtao6.png");
        pool.put(20, "img/hongtao7.png");
        pool.put(21, "img/hongtao8.png");
        pool.put(22, "img/hongtao9.png");
        pool.put(23, "img/hongtao10.png");
        pool.put(24, "img/hongtaoJ.png");
        pool.put(25, "img/hongtaoQ.png");
        pool.put(26, "img/hongtaoK.png");
        pool.put(27, "img/meihuaA.png");
        pool.put(28, "img/meihua2.png");
        pool.put(29, "img/meihua3.png");
        pool.put(30, "img/meihua4.png");
        pool.put(31, "img/meihua5.png");
        pool.put(32, "img/meihua6.png");
        pool.put(33, "img/meihua7.png");
        pool.put(34, "img/meihua8.png");
        pool.put(35, "img/meihua9.png");
        pool.put(36, "img/meihua10.png");
        pool.put(37, "img/meihuaJ.png");
        pool.put(38, "img/meihuaQ.png");
        pool.put(39, "img/meihuaK.png");
        pool.put(40, "img/fangpianA.png");
        pool.put(41, "img/fangpian2.png");
        pool.put(42, "img/fangpian3.png");
        pool.put(43, "img/fangpian4.png");
        pool.put(44, "img/fangpian5.png");
        pool.put(45, "img/fangpian6.png");
        pool.put(46, "img/fangpian7.png");
        pool.put(47, "img/fangpian8.png");
        pool.put(48, "img/fangpian9.png");
        pool.put(49, "img/fangpian10.png");
        pool.put(50, "img/fangpianJ.png");
        pool.put(51, "img/fangpianQ.png");
        pool.put(52, "img/fangpianK.png");
        pool.put(53, "img/guowang.png");
        pool.put(54, "img/wanghou.png");
        pool.put(55, "img/bb.png");
    }
}
